﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;


namespace Quanlybanhang_pro202.Module
{
    class Hoadon
    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["quanlybanhang"].ToString());
        SqlCommand cmd;
        SqlDataAdapter sqlDA;
        DataSet ds;
        DataTable dt;

        public DataSet GetAllInvoices()
        {
            try
            {
                conn.Open();
                cmd = new SqlCommand("select mahoadon as N'Mã hóa đơn',thungan as N'Thu Ngân' , makh as N'Mã khách hàng', ngaylap as N'Ngày xuất hóa đơn' from hoadon", conn);
                sqlDA = new SqlDataAdapter();
                ds = new DataSet();
                sqlDA.SelectCommand = cmd;
                sqlDA.Fill(ds, "hoadon");
                return ds;
            }
            catch
            {
                throw;
            }
        }
        public int InsertInvoice(string thungan, string ngay)
        {
            try
            {
                conn.Open();
                cmd = new SqlCommand("exec sp_themhoadon '" + thungan + "','" + ngay + "'", conn);
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    return 1;
                }
                return 0;

            }
            catch
            {
                throw;
            }
        }
        public int UpdateInvoices(string mahoadon, string thungan, int makh, string ngay)
        {
            try
            {
                conn.Open();
                cmd = new SqlCommand("update hoadon set thungan = '" + thungan + "',makh = '" + makh + "',ngaylap = '" + ngay + "' where mahoadon = '"+mahoadon+"'", conn);
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    return 1;
                }
                return 0;

            }
            catch
            {
                throw;
            }
        }

        public int DeleteInvoice(string mahoadon)
        {
            try
            {
                conn.Open();
                cmd = new SqlCommand("delete hoadon where mahoadon = '"+mahoadon+"'", conn);
                int i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    return 1;
                }
                return 0;

            }
            catch
            {
                throw;
            }
        }
    }
}
